<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="信息"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getSystemdictionarydetails">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="systemdictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="id" label="编号" width="120" sortable>
			</el-table-column>
			<el-table-column prop="name" label="名字" width="120" sortable>
			</el-table-column>
			<el-table-column prop="sn" label="产品编号" width="120" sortable>
			</el-table-column>

			<!--<el-table-column prop="manager.userusername" label="管理员" width="100" sortable>
			</el-table-column>
			<el-table-column prop="parent.username" label="上级部门" width="120" sortable>
			</el-table-column>-->
			<el-table-column label="操作" width="150">
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
			<!--抽屉-->
			<el-table-column label="产品表" width="150" sortable>
				<template>
					<el-button @click="drawer = true" type="primary" style="margin-left: 16px;">
						查看产品表
					</el-button>
					<el-drawer
							title="我是标题"
							:visible.sync="drawer"
							:with-header="false"
							size="50%">

						<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
							<el-form :inline="true" :model="filters">
								<el-form-item>
									<el-input v-model="filters.keyword" placeholder="信息"></el-input>
								</el-form-item>
								<el-form-item>
									<el-button type="primary" v-on:click="getSystemdictionarydetails">查询</el-button>
								</el-form-item>
								<el-form-item>
									<el-button type="primary" @click="handleAdd">新增</el-button>
								</el-form-item>
							</el-form>
						</el-col>+

						<!--抽屉里面的列表-->
						<el-table :data="systemdictionarydetails" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
							<el-table-column type="selection" width="55">
							</el-table-column>
							<el-table-column type="index" width="60">
							</el-table-column>
							<el-table-column prop="id" label="编号" width="120" sortable>
							</el-table-column>
							<el-table-column prop="name" label="名字" width="120" sortable>
							</el-table-column>
							<el-table-column prop="types_id" label="类型" width="120" sortable>
							</el-table-column>

							<!--<el-table-column prop="manager.userusername" label="管理员" width="100" sortable>
                            </el-table-column>
                            <el-table-column prop="parent.username" label="上级部门" width="120" sortable>
                            </el-table-column>-->
							<el-table-column label="操作" width="150">
								<template scope="scope">
									<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
									<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
								</template>
							</el-table-column>
						</el-table>




					</el-drawer>
				</template>
			</el-table-column>

		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
						   :page-size="pageSize" :total="total" :current-page="currentPage" style="float:right;">
			</el-pagination>
		</el-col>
		<!--
			rules：表单校验规则，参考elementui看具体可以有哪些规则
		-->
		<!--新增或编辑界面模态框-->
		<el-dialog :title="title" :visible.sync="systemdictionarydetailFormVisible" :close-on-click-modal="false">
			<el-form :model="systemdictionarydetailForm" label-width="80px" :rules="systemdictionarydetailFormRules" ref="systemdictionarydetailForm">
				<!--<el-form-item label="编号" prop="id">
					<el-input v-model="systemdictionarydetailForm.id" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="名字" prop="name">
					<el-input v-model="systemdictionarydetailForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="产品类型" prop="sn">
					<el-input v-model="systemdictionarydetailForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="systemdictionarydetailFormVisible = false">取消</el-button>
				<el-button type="primary" @click="systemdictionarydetailSubmit" :loading="systemdictionarydetailLoading">提交</el-button>
			</div>
		</el-dialog>

	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getSystemdictionarydetailListPage, removeSystemdictionarydetail, batchRemoveSystemdictionarydetail, systemdictionarydetailSystemdictionarydetail, addSystemdictionarydetail } from '../../api/api';

	export default {
        data: function () {
            return {
                title:'',
                filters: {
                    keyword: ''
                },
                systemdictionarydetails: [],
                total: 0,
                pageSize:5,
                currentPage: 1,
                listLoading: false,
                sels: [],//列表选中列
                drawer: false,

                systemdictionarydetailFormVisible: false,//编辑界面是否显示
                systemdictionarydetailLoading: false,
                systemdictionarydetailFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ],
                },
                //编辑界面数据
                systemdictionarydetailForm: {
                    id: null,
                    name: '',
                    sn: null,
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'}
                    ]
                },

            }
        },
		methods: {
			//性别显示转换
			formatSex: function (row, column) {
				return row.state == 0 ? '启用' : row.state == -1 ? '禁用' : '未知';
			},
			handleCurrentChange(currentPage) {
				this.currentPage = currentPage;
				this.getSystemdictionarydetails();
			},
			//获取部门列表
			getSystemdictionarydetails() {
				let para = {
					currentPage: this.currentPage,
                    keyword: this.filters.keyword,
                    pageSize:this.pageSize,
				};
				this.listLoading = true;// 开启盲等框
				this.$http.post("/dictype",para)
					.then(result=>{
					    result = result.data;

					    this.systemdictionarydetails = result.rows;
                        console.log(this.systemdictionarydetails);
                        this.total = result.total;

					})
					.catch(result=>{
					    console.log(result)
						alert("系统错误")
					})
				// 关闭盲等框
                this.listLoading = false;

				//NProgress.start();
				/*getSystemdictionarydetailListPage(para).then((res) => {
					this.total = res.data.total;
					this.systemdictionarydetails = res.data.systemdictionarydetails;
					this.listLoading = false;
					//NProgress.done();
				});*/
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
				    // 开启忙等框
                    this.listLoading = true;
					//NProgress.start();
					//  向后台发送axios请求
					this.$http.delete("/dictype/" + row.id)
						.then(result => {
						    // 获取数据
						    result = result.data;
						    if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
							}else {
                                this.$message({
                                    message: '删除失败',
                                    type: 'error'
                                });
							}
						    // 关闭盲等框
                            this.listLoading = false;
                            this.currentPage = 1;
                            // 刷新页面
                            this.getSystemdictionarydetails();
						})
						.catch(result => {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            this.currentPage = 1;
                            // 刷新页面
                            this.getSystemdictionarydetails();
						})

					/*removeSystemdictionarydetail(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getSystemdictionarydetails();
					});*/
				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
                this.title ='编辑',
				this.systemdictionarydetailFormVisible = true;
				// 数据回显
				this.systemdictionarydetailForm = Object.assign({}, row);
			},
			//显示新增界面
            // 清空模态框数据
            handleAdd: function () {
                this.title ='新增',
				this.systemdictionarydetailFormVisible = true;
				this.systemdictionarydetailForm = {
                    id: null,
                    name: '',
                    id: '',
                    sn: null,
				};
			},
			// 表单的提交
			systemdictionarydetailSubmit: function () {
			    // 开启校验表单
				this.$refs.systemdictionarydetailForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
						    // 开启忙等框
							this.systemdictionarydetailLoading = true;
							//NProgress.start();
							// 克隆数据
							let para = Object.assign({}, this.systemdictionarydetailForm);

							/*if(para.parent){
							    let parent = para.parent;
							    // 获取数组的最后一个字段，既自己的id
							    let pid = parent[parent.length-1];
                                para.parent = {"id" : pid}
							}*/

							this.$http.put("/dictype",para)
								.then(result =>{
								    // 关闭忙灯框
                                    this.systemdictionarydetailLoading = false;
								    // 关闭模态框
                                    this.systemdictionarydetailFormVisible = false;
                                    // 刷新页面
                                    this.getSystemdictionarydetails();
                                    result = result.data;
                                    if(result.success){
                                        this.$message({
                                            message: '操作成功',
                                            type: 'success'
                                        });
                                    }else {
                                        this.$message({
                                            message: '操作失败1',
                                            type: 'error'
                                        });
                                    }
								})
								.catch(result =>{
                                    // 关闭忙灯框
                                    this.systemdictionarydetailLoading = false;
                                    // 关闭模态框
                                    this.systemdictionarydetailFormVisible = false;
                                    this.$message({
                                        message: '操作失败2',
                                        type: 'error'
                                    });
								})
							/*para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							systemdictionarydetailSystemdictionarydetail(para).then((res) => {
								this.systemdictionarydetailLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['systemdictionarydetailForm'].resetFields();
								this.systemdictionarydetailFormVisible = false;
								this.getSystemdictionarydetails();
							});*/
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
				    // 开启忙灯框
					this.listLoading = true;
					this.$http.patch("/dictype",ids)
                        .then(result => {
                            // 获取数据
                            result = result.data;
                            if(result.success){
                                this.$message({
                                    message: '删除成功',
                                    type: 'success'
                                });
                            }else {
                                this.$message({
                                    message: '删除失败',
                                    type: 'error'
                                });
                            }
                            // 关闭盲等框
                            this.listLoading = false;
                            this.currentPage = 1;
                            // 刷新页面
                            this.getSystemdictionarydetails();
                        })
                        .catch(result => {
                            this.$message({
                                message: '删除失败',
                                type: 'error'
                            });
                            this.currentPage = 1;
                            // 关闭盲等框
                            this.listLoading = false;
                            // 刷新页面
                            this.getSystemdictionarydetails();
                        })
					//NProgress.start();
					/*let para = { ids: ids };
					batchRemoveSystemdictionarydetail(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getSystemdictionarydetails();
					});*/
				}).catch(() => {

				});
			},
            /*getSystemdictionarydetails(){
			    this.$http.get("/systemdictionarydetail")
					.then(result => {
					    result = result.data;
					    this.systemdictionarydetails = result;
					})
					.catch(result => {

					})
			},*/
		},
		mounted() {
			this.getSystemdictionarydetails();
		}
	}

</script>

<style scoped>

</style>